<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ page
	import="org.apache.commons.fileupload.FileItemIterator,org.apache.commons.fileupload.FileItemStream,org.apache.commons.fileupload.FileUploadException,org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page
	import="java.io.BufferedReader,java.io.IOException,java.io.InputStream,java.io.InputStreamReader;"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>

<link rel="stylesheet" type="text/css"
	href="js/lib/jqPlot/jquery.jqplot.css" />

<script type="text/javascript" src="js/lib/jqPlot/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/jquery.jqplot.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/plugins/jqplot.pieRenderer.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/plugins/jqplot.dateAxisRenderer.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/plugins/jqplot.categoryAxisRenderer.min.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/plugins/jqplot.pointLabels.min.js"></script>
<script type="text/javascript" src="js/lib/jqPlot/plugins/jqplot.barRenderer.min.js"></script>

<style type="text/css">
html,body {
	margin: 0px;
	padding: 0px;
	color: white;
}

#selectChartDiv {
	height: 10%;
}

#chartDiv {
	height: 90%;
	width: 100%;
}

.chartSelector {
	width: 20%;
	float: left;
	text-align: center;
	cursor: pointer;
}

.ChartDiv1TopBar {
	height: 10%;
}

.ChartDiv1TopBarElem {
	width: 20%;
	float: left;
	text-align: center;
	cursor: pointer;
	border: solid 1px grey;
	margin-left: -2px;
}

#ChartDiv1BottomDiv {
	height: 90%;
	width: 100%;
	margin-left: -2px;
}

#ChartDiv1BottomDiv .jqplot-point-label {
	display: none;
}

.RatingLeftDiv {
	margin-top: 3%;
	margin-left: 5%;
	width: 30%;
	float: left;
}

.RatingRightDiv {
	margin-top: 3%;
	margin-left: 5%;
	width: 60%;
	float: left;
}

.RatingBottomDiv {
	margin-top: 3%;
	width: 100%;
	text-align: center;
	font-size: 25px;
	font-weight: bold;
}
</style>
<script type="text/javascript">
			var res;
			$.jqplot.config.enablePlugins = true;

			function setChartDiv1() {
				$("#chartDiv").html("");
				
				var chartDiv = document.getElementById("chartDiv");
				var topDiv = document.createElement("div");
				topDiv.className = "ChartDiv1TopBar";

				var topDivElem1 = document.createElement("div");
				topDivElem1.className = "ChartDiv1TopBarElem";
				topDivElem1.innerHTML = "All";
				topDivElem1.onclick = setChartDiv1All;
				var topDivElem2 = document.createElement("div");
				topDivElem2.className = "ChartDiv1TopBarElem";
				topDivElem2.innerHTML = "Month";
				topDivElem2.onclick = setChartDiv1Month;
				var topDivElem3 = document.createElement("div");
				topDivElem3.className = "ChartDiv1TopBarElem";
				topDivElem3.innerHTML = "Week";
				topDivElem3.onclick = setChartDiv1Week;
				var topDivElem4 = document.createElement("div");
				topDivElem4.className = "ChartDiv1TopBarElem";
				topDivElem4.innerHTML = "Day";
				topDivElem4.onclick = setChartDiv1Day;
				var topDivElem5 = document.createElement("div");
				topDivElem5.className = "ChartDiv1TopBarElem";
				topDivElem5.innerHTML = "Hour";
				topDivElem5.onclick = setChartDiv1Hour;
				var topDivElem6 = document.createElement("div");
				topDivElem6.style.clear = "both";

				topDiv.appendChild(topDivElem1);
				topDiv.appendChild(topDivElem2);
				topDiv.appendChild(topDivElem3);
				topDiv.appendChild(topDivElem4);
				topDiv.appendChild(topDivElem5);
				topDiv.appendChild(topDivElem6);
				
				var bottomDiv = document.createElement("div");
				bottomDiv.id = "ChartDiv1BottomDiv";

				chartDiv.appendChild(topDiv);
				chartDiv.appendChild(bottomDiv);
				setChartDiv1All();
			}

			function setChartDiv1All() {
				$("#ChartDiv1BottomDiv").html("");
					var maxVal = 0;
					var line = new Array();
					var firstDate = res.activity[0].date.getTime();
					for( var i = 0; i < res.activity.length; i++) {
						var temp = res.activity[i];
						if(temp == undefined)
							line.push([new Date(firstDate + i * 60000), 0]);
						else {
							line.push([temp.date, temp.keys]);
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
					var d = new Date();

					if(line.length == 0)
						$("#ChartDiv1BottomDiv").html("No data for that Period");
					else
						$.jqplot('ChartDiv1BottomDiv', [line], {
							title: "Activity",
							series:[{showMarker:false}],
							axes:{xaxis:{min:line[0].date,
							 		     max:line[line.length-1].date ,
							 		     renderer:$.jqplot.DateAxisRenderer, 
					              		 tickOptions:{formatString:"%#m/%#d %#I:%#M %p"}},
						  		  yaxis:{min:0,
							  	 		 max:maxVal}
							}
						});
			}

			function setChartDiv1Month() {
				var d;
				var maxVal = 0;
				$("#ChartDiv1BottomDiv").html("");
				var line = new Array();
				var firstDate = res.activity[0].date.getTime();
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp == undefined)
						line.push([new Date(firstDate + i * 60000), 0]);
					else{
						var tempD = new Date();
						d = new Date(tempD.getTime() - 31*24*60*60*1000);
						if(temp.date > d) {
							line.push([temp.date, temp.keys]);
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
				}
				d = new Date();


				if(line.length == 0)
					$("#ChartDiv1BottomDiv").html("No data for that Period");
				else
					$.jqplot('ChartDiv1BottomDiv', [line], {
						title: "Activity",
						series:[{showMarker:false}],
						axes:{xaxis:{min:line[0].date,
						 		     max:line[line.length-1].date ,
						 		     renderer:$.jqplot.DateAxisRenderer, 
				              		 tickOptions:{formatString:"%#m/%#d %#I:%#M %p"}},
							  		 yaxis:{min:0,
								  	 		max:maxVal}
						}
					});
			}

			function setChartDiv1Week() {
				var d;
				var maxVal = 0;
				$("#ChartDiv1BottomDiv").html("");
				var line = new Array();
				var firstDate = res.activity[0].date.getTime();
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp == undefined)
						line.push([new Date(firstDate + i * 60000), 0]);
					else{
						var tempD = new Date();
						d = new Date(tempD.getTime() - 7*24*60*60*1000);
						if(temp.date > d) {
							line.push([temp.date, temp.keys]);
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
				}
				d = new Date();


				if(line.length == 0)
					$("#ChartDiv1BottomDiv").html("No data for that Period");
				else
					$.jqplot('ChartDiv1BottomDiv', [line], {
						title: "Activity",
						series:[{showMarker:false}],
						axes:{xaxis:{min:line[0].date,
						 		     max:line[line.length-1].date ,
						 		     renderer:$.jqplot.DateAxisRenderer, 
				              		 tickOptions:{formatString:"%#m/%#d %#I:%#M %p"}},
							  		 yaxis:{min:0,
								  	 		max:maxVal}
						}
					});
			}

			function setChartDiv1Day() {
				var d;
				var maxVal = 0;
				$("#ChartDiv1BottomDiv").html("");
				var line = new Array();
				var firstDate = res.activity[0].date.getTime();
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp == undefined)
						line.push([new Date(firstDate + i * 60000), 0]);
					else{
						var tempD = new Date();
						d = new Date(tempD.getTime() - 24*60*60*1000);
						if(temp.date > d) {
							line.push([temp.date, temp.keys]);
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
				}
				d = new Date();


				if(line.length == 0)
					$("#ChartDiv1BottomDiv").html("No data for that Period");
				else
					$.jqplot('ChartDiv1BottomDiv', [line], {
						title: "Activity",
						series:[{showMarker:false}],
						axes:{xaxis:{min:line[0].date,
						 		     max:line[line.length-1].date ,
						 		     renderer:$.jqplot.DateAxisRenderer, 
				              		 tickOptions:{formatString:"%#m/%#d %#I:%#M %p"}},
							  		 yaxis:{min:0,
								  	 		max:maxVal}
						}
					});
			}

			function setChartDiv1Hour() {
				var d;
				var maxVal = 0;
				$("#ChartDiv1BottomDiv").html("");
				var line = new Array();
				var firstDate = res.activity[0].date.getTime();
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp == undefined) {
						var tempNoDate = new Date(firstDate + i * 60000);
						if(tempNoDate > d){
							line.push([tempNoDate, 0]);
						}
					}
					else{
						var tempD = new Date();
						d = new Date(tempD.getTime() - (60*60*1000));
						if(temp.date > d) {
							line.push([temp.date, temp.keys]);
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
				}
				d = new Date();

				if(line.length == 0)
					$("#ChartDiv1BottomDiv").html("No data for that Period");
				else
					$.jqplot('ChartDiv1BottomDiv', [line], {
						title: "Activity",
						series:[{showMarker:false}],
						axes:{xaxis:{min:line[0].date,
						 		     max:line[line.length-1].date ,
						 		     renderer:$.jqplot.DateAxisRenderer, 
				              		 tickOptions:{formatString:"%#m/%#d %#I:%#M %p"}},
							  		 yaxis:{min:0,
								  	 		max:maxVal}
						}
					});
			}
			

			function setChartDiv2() {
				$("#chartDiv").html("");
				var line = new Array();
				
				for( var i in res.softActivity) {
					var temp = res.softActivity[i];
					line.push([temp.softName+" ("+ Math.round(temp.time*100)/100+"%)", temp.time]);
				}
				
				jQuery.jqplot('chartDiv', [line], 
					{
						title: 'Activity repartition',
						seriesDefaults:{
				            renderer:$.jqplot.PieRenderer,
				            rendererOptions: {showDataLabels: true, sliceMargin:1, startAngle: 180, diameter:240}
			            },
						legend: { show:true },
						grid: {background: 'Black',
							   borderWidth: 0}
					}
				);
			}
			
			function setChartDiv3() {
				$("#chartDiv").html("");
				var line = new Array();
				
				for( var i in res.websiteActivity) {
					var temp = res.websiteActivity[i];
					line.push([temp.siteName+" ("+ Math.round(temp.time*100)/100+"%)", temp.time]);
				}
				
				jQuery.jqplot('chartDiv', [line], 
					{
						title: 'Websites activity repartition', 
						seriesDefaults:{
				            renderer:$.jqplot.PieRenderer,
				            rendererOptions: {showDataLabels: true, sliceMargin:1, startAngle: 180, diameter:240}
			            },
						legend: { show:true },
						grid: {background: 'Black',
							   borderWidth: 0}
					}
				);
			}
			
			function setChartDiv4() {
				$("#chartDiv").html("");
				var line = new Array();
				var xAxisValues = new Array();
				
				for( var i in res.lettersFreq) {
					var temp = res.lettersFreq[i];
					line.push(temp.times);
					xAxisValues.push(temp.letter);
				}
				
				jQuery.jqplot('chartDiv', [line], {
					title: 'Keys frequency',
					seriesDefaults:{renderer:$.jqplot.BarRenderer},
					axes:{
						xaxis:{
							renderer:$.jqplot.CategoryAxisRenderer,
							ticks:xAxisValues
						},
						yaxis:{min:0}
					}
				});
			}
			
			function setChartDiv5() {
				var tot = 0;
				
				var wrapper = document.getElementById("chartDiv");
				wrapper.innerHTML = "";
				
				var leftDiv = document.createElement("div");
				leftDiv.className = "RatingLeftDiv";
				wrapper.appendChild(leftDiv);

				var rightDiv = document.createElement("div");
				rightDiv.className = "RatingRightDiv";
				wrapper.appendChild(rightDiv);
				
				var clearDiv = document.createElement("div");
				clearDiv.style.clear = "both";
				wrapper.appendChild(clearDiv);
				
				var bottomDiv = document.createElement("div");
				bottomDiv.className = "RatingBottomDiv";
				wrapper.appendChild(bottomDiv);
				
				var charPerMin = getMaxCharPerMin();
				if(charPerMin > 150)
					charPerMin = 150;
				var crit = document.createElement("div");
				crit.innerHTML = "Maximum characters/minute: " + parseInt(charPerMin/4) + "/50";
				leftDiv.appendChild(crit);
				tot +=  parseInt(charPerMin/4);
				
				var nonStopPeriod = getLonguestUninterruptedPeriod();
				if(nonStopPeriod > 100)
					nonStopPeriod = 100;
				crit = document.createElement("div");
				crit.innerHTML = "Longest active period: " + parseInt(nonStopPeriod/2) + "/50";
				leftDiv.appendChild(crit);
			 	tot += parseInt(nonStopPeriod/2);
				
			 	for(var i in res.achievements) {
			 		crit = document.createElement("div");
			 		var curAch = res.achievements[i];
			 		var pts = getAchievementPoints(curAch.name);
			 		tot += pts;
					crit.innerHTML = curAch.name + "(" + curAch.desc + "): " + pts;
					rightDiv.appendChild(crit);
			 	}
				
			 	if(tot > 100)
			 		tot = 100;
				crit = document.createElement("span");
				crit.innerHTML = "Total score: " + tot + "/100";
				bottomDiv.appendChild(crit);
			}
			
			function getAchievementPoints(title) {
				var ret;
				switch(title) {
					case "Found the keyboard": ret = 1;
						break;
					case "Keyboard smasher": ret = 2;
						break;
					case "Keyboard destroyer": ret = 3;
						break;
					case "Found me!": ret = 20;
						break;
					case "Watching my stats": ret = 4;
						break;
					case "Daddy!": ret = 10;
						break;
					case "Facebook is evil!": ret = -20;
						break;
					case "Prove me correct": ret = 5;
						break;
					case "(Lisp (srsly?))": ret = -10;
						break;
					default: ret = 0;
				}
				return ret;
			}
			
			function getMaxCharPerMin() {
				var d;
				var maxVal = 0;
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp != undefined) {
						var tempD = new Date();
						d = new Date(tempD.getTime() - 24*60*60*1000);
						if(temp.date > d) {
							if(parseInt(temp.keys) > maxVal) {
								maxVal = parseInt(temp.keys);
							}
						}
					}
				}
				return maxVal;
			} 
			
			function getLonguestUninterruptedPeriod() {
				var d;
				var maxLen = 0;
				var curLen = 0;
				for( var i = 0; i < res.activity.length; i++) {
					var temp = res.activity[i];
					if(temp != undefined) {
						curLen++;
						if(curLen > maxLen)
							maxLen = curLen;
					} else {
						curLen = 0;
					}
				}
				return maxLen;
				
			}
	</script>
</head>
<body>
	<%
			ServletFileUpload upload;
			FileItemIterator iter;
			FileItemStream item;
			InputStream stream;
			BufferedReader br;
			StringBuilder sb = null;
			String line;
			String error = "\"null\"";
			Boolean fail = true;
		
			if (ServletFileUpload.isMultipartContent(request)) {
		
				upload = new ServletFileUpload();
		
				try {
					iter = upload.getItemIterator(request);
					while (iter.hasNext()) {
						item = iter.next();
						stream = item.openStream();
						if (!item.isFormField()) {
							br = new BufferedReader(new InputStreamReader(stream));
							sb = new StringBuilder();
							line = null;
							
							while ((line = br.readLine()) != null) {
								fail = false;
								sb.append(line).append("@@@");
							}
							br.close();
						}
					}
		
				} catch (FileUploadException e) {
				}
			} else {
				error = "\"Non mulipart form\"";
			}
			
			if(sb == null) {
				error = "\"No file input sent\"";
			} else {
				if(sb.toString() == "" || sb.toString() == null)
					error = "\"No file sent or empty file\"";
			}
			
			if (fail) { %>
	Error!
	<%= error %>
	<%  } else { %>
	<script type="text/javascript">
					var rawRes = <%= ("\"" + sb.toString().replace("\"", "\\\"") + "\"") %>;
					var parts = rawRes.split("***@@@");
					var tmpArr;
					var cpt;
					res = new Object();

					letters = parts[0].split("@@@");
					cpt=1;
					tmpArr = new Array();
					tmpArr[0] = new Object();
					tmpArr[0].letter = "other";
					tmpArr[0].times = 0;
					for(var i in letters) {
						var letterCodeFreq = letters[i].split("___");
						if((parseFloat(letterCodeFreq[1]) * 100) < 2.0) {
							tmpArr[0].times += (parseFloat(letterCodeFreq[1]) * 100);
						} else {
							if(!isNaN(parseFloat(letterCodeFreq[1]))) {
								tmpArr[cpt] = new Object();
								tmpArr[cpt].letter = letterCodeFreq[0];
								tmpArr[cpt].times = (parseFloat(letterCodeFreq[1]) * 100);
								cpt++;
							}
						}
					}
					
					res.lettersFreq = tmpArr.sort(function(a , b){
						return b.times - a.times;
					});
					
					cpt = 1;
					var letters = parts[1].split("@@@");
					res.softActivity = new Array();
					res.softActivity[0] = new Object();
					res.softActivity[0].softName = "Other";
					res.softActivity[0].time = 0;
					for(var i in letters) {
						var activityFreq = letters[i].split("___");
						if((parseFloat(activityFreq[1]) * 100) < 2.0) {
							res.softActivity[0].time += (parseFloat(activityFreq[1]) * 100);
						} else {
							if(!isNaN(parseFloat(activityFreq[1]))) {
								res.softActivity[cpt] = new Object();
								res.softActivity[cpt].softName = activityFreq[0];
								res.softActivity[cpt].time = (parseFloat(activityFreq[1])*100);
								cpt++;
							}
						}
					}

					res.softActivity = res.softActivity.sort(function(a , b){
						return b.time - a.time;
					});

					var letters = parts[2].split("@@@");
					res.websiteActivity = new Array();
					for(var i in letters) {
						var activityFreq = letters[i].split("___");
						if(!isNaN(parseFloat(activityFreq[1]))) {
							res.websiteActivity[i] = new Object();
							res.websiteActivity[i].siteName = activityFreq[0];
							res.websiteActivity[i].time = (parseFloat(activityFreq[1])*100);
						}
					}

					res.websiteActivity = res.websiteActivity.sort(function(a , b){
						return b.time - a.time;
					});

					var first = true;
					var firstDate;
					var letters = parts[3].split("@@@");
					res.activity = new Array();
					for(var i in letters) {
						var keyDates = letters[i].split("___");
						var splitDate = keyDates[0].split(" ");
						var tempDate = new Date(splitDate[0], splitDate[1] - 1, splitDate[2], splitDate[3], splitDate[4]);
						if(first) {
							firstDate = tempDate.getTime()/60000;
							first = false;
						}
						var index = (tempDate.getTime()/60000) - firstDate;
						res.activity[index] = new Object();
						res.activity[index].date = tempDate;
						res.activity[index].keys = keyDates[1];
					}
					
					var achievements = parts[4].split("@@@");
					res.achievements = new Array();
					for(var i in achievements) {
						if(achievements[i].indexOf("___") != -1) {
							var achievementDesc = achievements[i].split("___");
							res.achievements[i] = new Object();
							res.achievements[i].name = achievementDesc[0];
							res.achievements[i].desc = achievementDesc[1];
						}
					}
					
				</script>
	<div id="selectChartDiv">
		<div class="chartSelector" onclick="setChartDiv1();">Activity/time</div>
		<div class="chartSelector" onclick="setChartDiv2();">Activity
			repartition</div>
		<div class="chartSelector" onclick="setChartDiv3();">Websites
			activity repartition</div>
		<div class="chartSelector" onclick="setChartDiv4();">Letters
			repartition</div>
		<div class="chartSelector" onclick="setChartDiv5();">Rating</div>
		<div style="clear: both;"></div>
	</div>
	<div id="chartDiv"></div>
	<%  } %>


</body>
</html>